import Lang from "./lang";

export default class Url {

    static parseQuery2Obj(url) {
        const regArr = url.match(/[^?]*\?([^#]*)/);
        const params = regArr && regArr[1];
        if (!regArr) return;
        const obj = Object.create(null);
        params
            .split("&")
            .forEach(pair => {
                const [key, value] = pair.split("=");
                obj[key] = value;
            });
        return obj;
    }

    static parseObj2Query(obj) {
        return Lang
            .mapObj(obj, function (value, key) {
                return `${key}=${value}`
            })
            .reduce(function (acc, cur) {
                return acc ? `${acc}&${cur}` : `${acc}${cur}`
            }, "")
    }
}

